<?php
class Book
{
	public $bookID;
	public $bookCode;
	public $bookName;
	public $bookAuthor;
	public $bookNowQty;		//图书当前库存
	public $bookQty;		//图书应有总数

	public function __construct()
	{
		$this->bookID = null;
		$this->bookCode = '';
		$this->bookName = '';
		$this->bookAuthor = '';
		$this->bookNowQty = '';
		$this->bookQty = '';
	}

	public static function GetByBookCode($bookCode)
	{
		$book = new Book();
		$sql = "select * from lib_books where lib_books_code = $bookCode";
		$result = mysqli_query($GLOBALS['DB'], $sql);
		$row = mysqli_fetch_assoc($result);

		if(mysqli_num_rows($result) > 0){

			$book->bookID = $row['lib_books_id'];
			$book->bookCode = $row['lib_books_code'];
			$book->bookName = $row['lib_books_name'];
			$book->bookAuthor = $row['lib_books_author'];
			$book->bookNowQty = $row['lib_books_nqty'];
			$book->bookQty = $row['lib_books_qty'];

			mysqli_free_result($result);
			return $book;
		}else{
			return false;
		}
		
	}

	public static function GetByBookID($bookID)
	{
		$book = new Book();
		$sql = "select * from lib_books where lib_books_id = $bookID";
		$result = mysqli_query($GLOBALS['DB'], $sql);
		$row = mysqli_fetch_assoc($result);

		$book->bookID = $row['lib_books_id'];
		$book->bookCode = $row['lib_books_code'];
		$book->bookName = $row['lib_books_name'];
		$book->bookAuthor = $row['lib_books_author'];
		$book->bookNowQty = $row['lib_books_nqty'];
		$book->bookQty = $row['lib_books_qty'];		

		mysqli_free_result($result);

		return $book;

	}

	public function SaveBook()
	{
		if($this->bookID){
			$sql = "update lib_books set
				lib_books_id = $this->bookID, 
				lib_books_code = '$this->bookCode',
				lib_books_name = '$this->bookName', 
				lib_books_author = '$this->bookAuthor',
				lib_books_nqty = lib_books_nqty+($this->bookQty-lib_books_qty), 
				lib_books_qty = $this->bookQty
				where 
				lib_books_id = $this->bookID";
			if(mysqli_query($GLOBALS['DB'], $sql)){
				return 2;
			}else{
				return false;
			}
		}else{
			$sql = "insert into lib_books values (
				NULL, 
				'$this->bookCode',
				'$this->bookName', 
				'$this->bookAuthor', 
				$this->bookNowQty, 
				$this->bookQty
				)";
			if(mysqli_query($GLOBALS['DB'], $sql)){
				$this->bookID = mysqli_insert_id($GLOBALS['DB']);
				return 1;
			}else{
				return false;
			}
		}
	}

	public static function DeleteBook($bookCode)
	{
		$book = new Book();
		$sql = "update lib_books set
			lib_books_nqty = 0,
			lib_books_qty = 0
			where
			lib_books_code = '$bookCode'";
		// $sql = "delete from lib_books where lib_books_code = $bookCode";
		mysqli_query($GLOBALS['DB'], $sql);

		if(mysqli_affected_rows($GLOBALS['DB']) > 0){
			return 3;
		}else{
			return false;
		}
	}

	public function RecordHistory($type)
	{
		$sql = "insert into lib_history values (
			NULL,
			'$type',
			'0',
			'$this->bookID',
			now()
			)";
		if(mysqli_query($GLOBALS['DB'], $sql)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 *	根据不同参数获取相关操作历史记录
	 *  $key=1 根据图书条码
	 *  $key=2 根据用户姓名
	 */
	public static function ListHisory($key, $val)
	{	
		if ($key == 1) {
			$key = "info_history_bcode";
		}elseif ($key == 2) {
			$key = "info_history_uname";
		}

		$sql = "select * from lib_historyInfo where $key = '$val' 
			order by info_history_id desc";
		$result = mysqli_query($GLOBALS['DB'], $sql);

		$historyArray = array();
		$i = 0;

		if(mysqli_num_rows($result) > 0){
			while($row = mysqli_fetch_assoc($result))
			{	

				switch ($row['info_history_type']) {
					case '1':
						$row['info_history_type'] = "添加";
						break;
					case '2':
						$row['info_history_type'] = "修改";
						break;
					case '3':
						$row['info_history_type'] = "删除";
						break;
					case '4':
						$row['info_history_type'] = "借阅";
						break;
					case '5':
						$row['info_history_type'] = "归还";
						break;
				}

				$historyArray[$i]['hid'] = $row['info_history_id'];
				$historyArray[$i]['htype'] = $row['info_history_type'];
				$historyArray[$i]['huid'] = $row['info_history_uid'];
				$historyArray[$i]['huname'] = $row['info_history_uname'];
				$historyArray[$i]['hbid'] = $row['info_history_bid'];
				$historyArray[$i]['hbname'] = $row['info_history_bname'];
				$historyArray[$i]['hbcode'] = $row['info_history_bcode'];
				$historyArray[$i]['htime'] = $row['info_history_time'];
				
				$i++;
			}
			mysqli_free_result($result);
			return json_encode($historyArray);
		}else{
			return false;
		}


	}
		
}
?>